<template>
	<view class="search">

		<view class="header">
			<uni-icons class="icon" type="back" size="50rpx" color="#000" @click="backHandler"></uni-icons>

			<uni-search-bar @confirm="search" v-model="searchValue" @clear="clear" @cancel=cancel>
			</uni-search-bar>


		</view>
		<view class="section" v-if="active">
			<view class="onenode">
				<view class="title">
					<text>热门搜索</text>
				</view>
				<view class="node">
					<view v-for="(item, index) in hots" :key="index" class="item" @click="searchHandler(item)">
						{{ item }}
					</view>
				</view>
			</view>

			<view class="onenode">

				<view class="title">
					<text>历史搜索</text>
					<uni-icons class="icon" type="trash-filled" size="40rpx" color="#000" @click="delHistry">
					</uni-icons>
				</view>


				<view class="node" v-if="seacrchhistory.length">
					<view class="item" v-for="(o, index) in seacrchhistory" :key="index + 'o'"
						@click="searchHandler(o)">
						{{ o }}

					</view>
				</view>
				<view class="node" v-else>暂无数据</view>
			</view>
		</view>

		<view class="section" v-else>
			<view v-for="(opp, index) in searchList" :key="index + 'oop'" class="oop" @click="toDetail(opp._id)">
				<view class="title">
					<view>{{ opp.title }}</view>
					<text>{{ opp.salary }}</text>
				</view>
				<view class="text">
					{{ opp.workAddress }}| {{ opp.workRange }}|
					{{ opp.education }}
				</view>
				<view class="list">
					<view v-for="(work, index) in opp.welfare" :key="index + 'work'">
						{{ work }}
					</view>
				</view>
				<view class="vantimg">
					<view class="smallvant">
						<!-- <img :src="opp.companyInfo[0].logoUrl" alt="" /> -->
						<image :src="opp.companyInfo[0].logoUrl" mode=""></image>
						<view class="smalltitle">
							<text>{{ opp.companyInfo[0].title }}</text>
							<view>
								{{ opp.companyInfo[0].companyType }}
								{{ opp.companyInfo[0].companyNumber }}
							</view>
						</view>
					</view>
					<view class="bottoms" @click.stop="dialogToggle(index)">
						立即投递
					</view>

				</view>
				
				<view>
					<!-- 提示窗示例 -->
					<uni-popup ref="alertDialog" type="dialog">
						<uni-popup-dialog :type="msgType" cancelText="关闭" confirmText="确认" title="通知" content="是否投递给该公司!"
							@confirm="dialogConfirm(opp)" @close="dialogClose"></uni-popup-dialog>
					</uni-popup>
				</view>
			</view>
			
			
		</view>
		
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value: "",
				searchValue: "",
				hot: [],
				guess: [],
				searchList: [],
				seacrchhistory: JSON.parse(localStorage.getItem("seacrchhistory")) || [],
				active: true,
				hots: ["前端", "goLang", "PHP"],
				list: null,
				works: [],
				type: 'center',
				msgType: 'success',
				sendList: JSON.parse(localStorage.getItem("sendList")) || [], //投递列表
			};
		},

		methods: {
			backHandler() {
				uni.navigateBack()
			},

			toDetail(id) {
				uni.navigateTo({
					url: '/pages/detail/detail?id=' + id
				});

			},
			open() {
				// 通过组件定义的ref调用uni-popup方法 ,如果传入参数 ，type 属性将失效 ，仅支持 ['top','left','bottom','right','center']
				this.$refs.popup.open('bottom')
			},
			dialogToggle(index) {
				let token = localStorage.getItem("token")
				console.log(token);
				// console.log(index);
				if (token != "") {
					this.msgType = 'success';
					// console.log(this.$refs.alertDialog);
					this.$refs.alertDialog[index].open()
				} else {
					uni.navigateTo({
						url: '/pages/login/login'
					});

				}

			},
			dialogConfirm(item) {
				console.log('点击确认');
				console.log(item);


				// this.sendList = [...this.sendList,item];
				this.sendList.push(item);

				//去重
				let newArr = [];
				for (let i = 0; i < this.sendList.length; i++) {
					if (newArr.indexOf(this.sendList[i]._id) == -1) {
						newArr.push(this.sendList[i]._id);
					} else {
						this.sendList.splice(i, 1);
					};
				};

				console.log(this.sendList);
				localStorage.setItem("sendList", JSON.stringify(this.sendList));

				//跳转到聊天页面
				uni.navigateTo({
					url: '/pages/call/call?workid=' + item._id
				});




			},
			dialogClose() {
				console.log('点击关闭')
			},

			//输入框回车
			search(res) {

				if (res.value) {
					this.active = false;
					this.seacrchhistory.unshift(res.value);
					this.seacrchhistory = new Set(this.seacrchhistory);
					this.seacrchhistory = [...this.seacrchhistory];
					uni.request({
						url: `https://yw.52kfw.cn/api/v1/company/work-list?size=20&isRecommend=1&kw=${res.value}`
					}).then((res) => {
						this.searchList = res.data.data.list;
						if (this.searchList.length == 0) {
							uni.showToast({
								title: '没有找到相关岗位',
								icon: 'none'
							})
						}
						console.log(this.searchList);
					}).catch(() => {
						return;
					});
				} else {
					uni.showToast({
						title: '搜索内容不能为空',
						icon: 'none'
					})
				}

			},
			//点击热门与历史搜索
			searchHandler(value) {
				this.searchValue = value
				if (value) {
					this.active = false;
					this.seacrchhistory.unshift(value);
					this.seacrchhistory = new Set(this.seacrchhistory);
					this.seacrchhistory = [...this.seacrchhistory];
					uni.request({
						url: `https://yw.52kfw.cn/api/v1/company/work-list?size=20&isRecommend=1&kw=${value}`
					}).then((res) => {
						this.searchList = res.data.data.list;
						if (this.searchList.length == 0) {
							uni.showToast({
								title: '没有找到相关岗位',
								icon: 'none'
							})
						}
						console.log(this.searchList);
					}).catch(() => {
						return;
					});
				} else {
					uni.showToast({
						title: '搜索内容不能为空',
						icon: 'none'
					})
				}

			},
			//clear事件
			clear(res) {

				this.active = true;
			},

			//点击取消
			cancel(res) {

				this.active = true;
			},
			//清除历史记录
			delHistry() {
				this.seacrchhistory = []
			},
			onBackPress() {
				// #ifdef APP-PLUS
				plus.key.hideSoftKeybord();
				// #endif
			},



		},
		watch: {
			seacrchhistory: {
				handler(newValue) {
					console.log(newValue);
					localStorage.setItem("seacrchhistory", JSON.stringify([...newValue]));
				},
				deep: true,
			},


		},
	};
</script>

<style lang="scss">
	.search {
		width: 100vw;
		box-sizing: border-box;

		.header {
			display: flex;
			// justify-content: space-between;
			align-items: center;
			height: 120rpx;
			padding: 30rpx 30rpx;
			position: sticky;
			background-color: #fff;
			top: 0;
			right: 0;
			left: 0;
			box-shadow: 2rpx 2rpx 2rpx 2rpx #f4f4f4;
			z-index: 999;

			.form {
				width: 80%;
			}
		}

		.section {
			width: 100%;
			padding: 30rpx 40rpx;
			// background-color: #f4f4f4;

			.oop {
				width: 95%;
				margin-top: 30rpx;
				// height: 250rpx;
				border-radius: 20rpx;
				box-shadow: 2rpx 2rpx 4rpx 4rpx #f4f4f4;
				padding: 0 20rpx;
				background-color: #fff;
				margin: 0 auto;
				margin-bottom: 40rpx;
				padding-bottom: 20rpx;
				z-index: -999;

				.title {
					display: flex;
					justify-content: space-between;
					font-size: 36rpx;
					font-family: SourceHanSansCN;
					font-weight: 400;
					color: #333333;
					line-height: 70rpx;

					text {
						color: #5fae53;
					}
				}

				.text {
					color: #999;
					line-height: 40rpx;
					text-align: start;
					font-size: 24rpx;

				}

				.list {
					display: flex;
					flex-wrap: wrap;
					margin-top: 10rpx;

					view {
						display: inline-block;
						height: 36rpx;
						text-align: center;
						line-height: 36rpx;
						padding: 0 8rpx;
						margin-right: 10rpx;
						background: #f4f4f4;
						border-radius: 8rpx;
						font-size: 24rpx;
						margin-bottom: 10rpx;
					}
				}

				.vantimg {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.smallvant {
						display: flex;
						align-items: center;
						margin-top: 20rpx;

						image {
							width: 60rpx;
							height: 60rpx;
							border-radius: 50%;
						}

						.smalltitle {
							text-align: start;
							margin-left: 20rpx;


							text {
								font-size: 28rpx;
							}

							view {
								font-size: 24rpx;
								color: #999;
							}
						}
					}

					.bottoms {
						width: 130rpx;
						height: 50rpx;
						background: #ffffff;
						border: 2rpx solid #5fae53;
						border-radius: 26rpx;
						text-align: center;
						line-height: 48rpx;
						font-size: 24rpx;
						color: #5fae53;
					}
				}
			}

			.onenode {
				margin-bottom: 30rpx;

				.title {
					display: flex;
					justify-content: space-between;
					align-items: center;

					// height: 60rpx;
					text {
						font-size: 32rpx;
						// font-family: SourceHanSansCN;
					}

					.icon {
						font-weight: normal;
					}
				}

				text {
					font-weight: bold;
					font-size: 24rpx;
					// font-family: SourceHanSansCN;
					margin: 30rpx 0;
					text-align: start;
				}

				.node {
					display: flex;
					justify-content: flex-start;
					flex-wrap: wrap;
					// margin-top: 30rpx;

					.item {
						// width: 140rpx;
						padding: 0rpx 16rpx;
						height: 60rpx;
						line-height: 60rpx;
						background: #eeeeee;
						border-radius: 20rpx;
						margin-right: 30rpx;
						// position: relative;
						font-size: 24rpx;

						// .iocn {
						// 	position: absolute;
						// 	top: -10rpx;
						// 	right: 0;
						// }
					}
				}
			}
		}

		// .uni-searchbar__box{
		// 	width: 480rpx;
		// }

		.uni-searchbar {
			width: 640rpx;
		}

		.search-result {
			padding-top: 20rpx;
			padding-bottom: 40rpx;
			text-align: center;
		}

		.search-result-text {
			text-align: center;
			font-size: 28rpx;
			color: #666;
		}

		.example-body {
			/* #ifndef APP-NVUE */
			display: block;
			/* #endif */
			padding: 0px;
		}

		.uni-mt-10 {
			margin-top: 20rpx;
		}



	}
</style>
